﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYView;

namespace LightSmokeTemperatureCameraAdjust
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	internal class CmdElementAdjust : YJKCommandManual
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
            CmdElementAdjust.m_hWndRevit = new HYCodeBase.WinForm.WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			List<ViewPlan> source = new List<ViewPlan>();
			List<ElementId> lstViewId = new List<ElementId>();
			MoveElementOperator moveElementOperator = new MoveElementOperator();
			for (;;)
			{
				ElementAdjustForm elementAdjustForm = new ElementAdjustForm();
				if (elementAdjustForm.ShowDialog() != DialogResult.OK)
				{
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				new List<KeyValuePair<BuiltInCategory, string>>();
				if (elementAdjustForm.FormDataSaver.m_bIsSelectOrProject)
				{
					for (;;)
					{
						ElementNeedMoveAnalyzer elementNeedMoveAnalyzer = new ElementNeedMoveAnalyzer();
						try
						{
							elementNeedMoveAnalyzer.GetElements(activeUIDocument, elementAdjustForm.FormDataSaver.m_lstStrCategory, 1, null);
						}
						catch (Exception)
						{
							break;
						}
						CeilingOrFloorSearcher useSearcher = new CeilingOrFloorSearcher(document);
						moveElementOperator.MoveElement(document, elementNeedMoveAnalyzer.LstElement, useSearcher, elementAdjustForm);
					}
				}
				else
				{
					ElementNeedMoveAnalyzer elementNeedMoveAnalyzer2 = new ElementNeedMoveAnalyzer();
					if (!FloorSelectManager.SelectFloorViewPlans(document, ref source))
					{
						break;
					}
					lstViewId = (from n in source
					select n.GenLevel.Id).ToList<ElementId>();
					elementNeedMoveAnalyzer2.GetElements(activeUIDocument, elementAdjustForm.FormDataSaver.m_lstStrCategory, 0, lstViewId);
					CeilingOrFloorSearcher useSearcher2 = new CeilingOrFloorSearcher(document);
					moveElementOperator.MoveElement(document, elementNeedMoveAnalyzer2.LstElement, useSearcher2, elementAdjustForm);
				}
				if (!elementAdjustForm.FormDataSaver.m_bIsSelectOrProject)
				{
					return Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

        public static HYCodeBase.WinForm.WindowHandle m_hWndRevit;
	}
}
